package com.ar.studyapp;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.ar.datahandler.StringJSONHandler;
import com.ar.network.NetworkUtility;
import com.ar.network.OnResponseListener;
import com.ar.network.RequestBuilder;
import com.ar.network.SOAPConnector;

public class LevelOptionsActivity extends BaseActivity implements
		OnClickListener, OnResponseListener {

	private Button btnLvlHighSchoolEntrance;
	private Button btnLvlUniversityEntrance;
	private Button btnLvlRandom;
	private StringJSONHandler jsonWorker;
	private String titleToPut;
	private ProgressDialog progressDialogOnLoad;

	private String category_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void displayActivity() {
		setContentView(R.layout.activity_level_options);
	}

	@Override
	protected void initViews() {
		btnLvlHighSchoolEntrance = (Button) findViewById(R.id.btnLvlHighSchoolEntrance);
		btnLvlUniversityEntrance = (Button) findViewById(R.id.btnLvlUniversityEntrance);
		btnLvlRandom = (Button) findViewById(R.id.btnLvlRandom);

	}

	@Override
	protected void initVariables() {
		jsonWorker = new StringJSONHandler();
	}

	@Override
	protected void initActions() {
		setTitle(getIntent().getStringExtra("title"));
		String difficults = getIntent().getStringExtra("difficults");
		btnLvlHighSchoolEntrance.setOnClickListener(this);
		btnLvlUniversityEntrance.setOnClickListener(this);
		btnLvlRandom.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		category_id = this.getIntent().getStringExtra("categoryId");
		if (view == btnLvlHighSchoolEntrance) {
			if (NetworkUtility.checkNetworkState(this)) {
				titleToPut = new String(getTitle() + " - " + "Hard");
				startRequest(category_id, 1);
			} else
				NetworkUtility.showNoNetworkDialog(this);
		} else if (view == btnLvlUniversityEntrance) {
			if (NetworkUtility.checkNetworkState(this)) {
				titleToPut = new String(getTitle() + " - " + "Medium");
				startRequest(category_id, 5);
			} else
				NetworkUtility.showNoNetworkDialog(this);
		} else if (view == btnLvlRandom) {
			if (NetworkUtility.checkNetworkState(this)) {
				titleToPut = new String(getTitle() + " - " + "Easy");
				startRequest(category_id, 10);
			} else
				NetworkUtility.showNoNetworkDialog(this);
		}
	}

	private void startRequest(String category_id, int category_type) {
		SOAPConnector connector = new SOAPConnector(
				RequestBuilder.BuildOnLoadRequest(category_id, ""
						+ category_type));
		connector.setOnResponseListener(this);
		progressDialogOnLoad = ProgressDialog.show(this, "Loading exam",
				"Please wait …");
		connector.startRequest();

	}

	@Override
	public void onResponse(String jsonResponse) {
		progressDialogOnLoad.dismiss();

		if (category_id.equals("3") || category_id.equals("4")
				|| category_id.equals("5") || category_id.equals("6")
				|| category_id.equals("7")) {
			try {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put(NetworkUtility.HANDLE, NetworkUtility.ONERROR);
				jsonObject.put(NetworkUtility.ERROR_CODE, "1");
				jsonObject.put(NetworkUtility.ERROR_MESSAGE, "Data not found!");
				jsonResponse = jsonObject.toString();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		if (jsonWorker.checkError(jsonResponse) == false) {
			// Log.d("onLoad", jsonResponse.toString());
			Intent intent = new Intent(this, ExamActivity.class);
			intent.putExtra("title", titleToPut);
			intent.putExtra("data", jsonResponse.toString());
			startActivity(intent);
		} else {
			if(jsonWorker.getErrorCode(jsonResponse).equals("1")) NetworkUtility.showDialog(this, "Error", jsonWorker.getErrorMessage(jsonResponse));
		}
	}

	@Override
	public void onRequestTimeOut() {
		// Intent intent = new Intent(this, ExamActivity.class);
		// intent.putExtra("title", titleToPut);
		// intent.putExtra("data", jsonObject.toString());
		progressDialogOnLoad.dismiss();
		NetworkUtility.showTimeOutDialog(this);
		// startActivity(intent);

	}

}
